using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Bll
{
	public class UserLotteryCardBll
	{
		private static UserLotteryCardBll _instance = null;
		private readonly UserLotteryCardDal dal = new UserLotteryCardDal();
		public static UserLotteryCardBll instance
		{
			get
			{
				if (UserLotteryCardBll._instance == null)
				{
					UserLotteryCardBll._instance = new UserLotteryCardBll();
				}
				return UserLotteryCardBll._instance;
			}
		}
		public void Add(UserLotteryCardModel model, params Conn[] conn)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserLotteryCard(");
			stringBuilder.Append("CardNumber,FaceValue,Balance,EffectiveTime,UserID,IsDeleted,remark");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@CardNumber,@FaceValue,@Balance,@EffectiveTime,@UserID,@IsDeleted,@remark");
			stringBuilder.Append(") ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@EffectiveTime", SqlDbType.DateTime),
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@IsDeleted", SqlDbType.Bit, 1),
				new SqlParameter("@remark", SqlDbType.Text)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.FaceValue;
			array[2].Value = model.Balance;
			array[3].Value = model.EffectiveTime;
			array[4].Value = model.UserID;
			array[5].Value = model.IsDeleted;
			array[6].Value = model.remark;
			if (conn.Length > 0)
			{
				conn[0].ExecuteTransaction(stringBuilder.ToString(), array);
			}
			else
			{
				Conn.ExecuteSql(stringBuilder.ToString(), array);
			}
		}
		public bool Exists(string CardNumber)
		{
			return this.dal.Exists(CardNumber);
		}
		public void Add(UserLotteryCardModel model)
		{
			this.dal.Add(model);
		}
		public bool Update(UserLotteryCardModel model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(string CardNumber)
		{
			return this.dal.Delete(CardNumber);
		}
		public UserLotteryCardModel GetModel(string CardNumber)
		{
			return this.dal.GetModel(CardNumber);
		}
		public UserLotteryCardModel GetModelByCache(string CardNumber)
		{
			string cacheKey = "UserLotteryCardModel_" + CardNumber;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(CardNumber);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (UserLotteryCardModel)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<UserLotteryCardModel> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<UserLotteryCardModel> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<UserLotteryCardModel> list = new System.Collections.Generic.List<UserLotteryCardModel>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					UserLotteryCardModel userLotteryCardModel = new UserLotteryCardModel();
					userLotteryCardModel.CardNumber = dt.Rows[i]["CardNumber"].ToString();
					if (dt.Rows[i]["FaceValue"] != null && dt.Rows[i]["FaceValue"].ToString() != "")
					{
						userLotteryCardModel.FaceValue = new decimal?(decimal.Parse(dt.Rows[i]["FaceValue"].ToString()));
					}
					if (dt.Rows[i]["Balance"] != null && dt.Rows[i]["Balance"].ToString() != "")
					{
						userLotteryCardModel.Balance = new decimal?(decimal.Parse(dt.Rows[i]["Balance"].ToString()));
					}
					if (dt.Rows[i]["EffectiveTime"] != null && dt.Rows[i]["EffectiveTime"].ToString() != "")
					{
						userLotteryCardModel.EffectiveTime = new System.DateTime?(System.DateTime.Parse(dt.Rows[i]["EffectiveTime"].ToString()));
					}
					if (dt.Rows[i]["UserID"] != null && dt.Rows[i]["UserID"].ToString() != "")
					{
						userLotteryCardModel.UserID = new int?(int.Parse(dt.Rows[i]["UserID"].ToString()));
					}
					if (dt.Rows[i]["IsDeleted"] != null && dt.Rows[i]["IsDeleted"].ToString() != "")
					{
						if (dt.Rows[i]["IsDeleted"].ToString() == "1" || dt.Rows[i]["IsDeleted"].ToString().ToLower() == "true")
						{
							userLotteryCardModel.IsDeleted = new bool?(true);
						}
						else
						{
							userLotteryCardModel.IsDeleted = new bool?(false);
						}
					}
					userLotteryCardModel.remark = dt.Rows[i]["remark"].ToString();
					list.Add(userLotteryCardModel);
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public DataSet GetListRowid(int Top, string strWhere, string filedOrder, params string[] selectField)
		{
			return this.dal.GetListRowid(Top, strWhere, filedOrder, selectField);
		}
		public DataSet PageOrderList(int PageSize, int PageIndex, string Orderfld, int OrderType, string strWhere)
		{
			return Conn.ExecProSpPageOrder("MBF_UserLotteryCard", " ROW_NUMBER() OVER (ORDER BY CardNumber ASC) rowid,*", PageSize, PageIndex, Orderfld, OrderType, strWhere);
		}
	}
}
